package com.zzg.pattern.factory.enumerate;

public enum OfficeEnum {
	EXCEL("excel", 1), PPT("PPT", 2), WORLD("world", 3);
	// 枚举属性
	private String name;
	private int index;

	// 构造函数
	private OfficeEnum(String name, int index) {
		this.name = name;
		this.index = index;
	}
	// set 和 get 方法
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}
	
	/**
	 * 根据name 获取index
	 */
	public static int getIndexByName(String name){
		OfficeEnum[] enums = OfficeEnum.values();
		for(OfficeEnum office : enums){
			if(office.getName().equalsIgnoreCase(name)){
				return office.getIndex();
			}
		}
		return 0;
	}
	
	/**
	 * 根据index 获取name
	 */
	public static String getNameByIndex(int index) {
		OfficeEnum[] enums = OfficeEnum.values();
		for(OfficeEnum office : enums){
			/**
			 * 代码总结：Integer 对象判断使用:equals()函数,int 对象判断使用:==
			 */
			if(office.getIndex() == index){
				return office.getName();
			}
		}
		return "";
	}
}
